package com.fengba.nanzhi.tabbar01;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.fengba.nanzhi.fengba.R;

import org.json.JSONArray;
import org.json.JSONObject;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;


public class FXDetailedAdapter extends RecyclerView.Adapter<FXDetailedAdapter.LinerViewHolder> {
    private Context context;
    private JSONArray jsonArray;
    private FX_FD_Detailed notice;
    private JSONObject job;
    private String type_ ="";


    public FXDetailedAdapter(Context context, FX_FD_Detailed add) {
        this.context = context;
        this.notice = add;
    }

    @Override
    public FXDetailedAdapter.LinerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new FXDetailedAdapter.LinerViewHolder(LayoutInflater.from(context).inflate(R.layout.fx_detailed_view,parent,false));
    }



    @Override
    public void onBindViewHolder(FXDetailedAdapter.LinerViewHolder holder, int position) {
        try {
            //id":7978,"user_name":"范想","user_account":"13597611231","type":5,"credit":0,"old_credits":1,"change_credits":1,"now_credits":0,"rule":"疯豆兑换游戏币","createtime":"2018-04-04 21:54:20"},
            job = (JSONObject) jsonArray.get(position);
            String data= ConverToDate(job.get("createtime").toString());
            holder.FDtv02.setText(job.get("rule").toString());

            if (job.get("credit").toString().equals("0"))
            {
             type_="-";
            }else
             {
                 type_="+";
             }
            holder.FDVtv01.setText(type_+""+job.get("change_credits").toString());
            holder.FDtv03.setText(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemCount() {
        jsonArray = notice.getJsonArray();
        ////System.out.println("aaaaaa"+jsonArray.toString());
        if(jsonArray!=null) {
            return jsonArray.length();
        }else
        {
            return 0;
        }
    }

    class LinerViewHolder extends RecyclerView.ViewHolder{
        private TextView FDVtv01;
        private TextView FDtv02;
        private TextView FDtv03;
        public LinerViewHolder(View itemView) {
            super(itemView);
            FDVtv01 = itemView.findViewById(R.id.FDVtv01);
            FDtv02 = itemView.findViewById(R.id.FDtv02);
            FDtv03 = itemView.findViewById(R.id.FDtv03);
        }
    }

    public final String ConverToDate(String strDate) throws Exception
    {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date d= df.parse(strDate);
        String time = df.format(d);
        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String date = sDateFormat.format(new Date());
        if (time.equals(date))
        {
            DateFormat df1 = new SimpleDateFormat("hh:mm");
            Date d1= df.parse(strDate);
            String a= df1.format(d1);
            return " 今天:"+a+" ";
        }else
        {
            return " "+time+" ";
        }

    }
}

